package com.shunbo.convenient.service;

import com.shunbo.convenient.utils.DateUtils;
import com.shunbo.convenient.vo.*;
import org.apache.commons.lang.StringUtils;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import static com.shunbo.convenient.utils.DateUtils.compare_date;


@Component
public class ScheduledService {

    @Resource
    private CouponService couponService;

    @Resource
    private CouponDroolService couponDroolService;

    @Resource
    private CouponOrderService couponOrderService;

    @Resource
    private UserCouponService userCouponService;

    @Resource
    private OrdersService ordersService;

    @Scheduled(cron = "0 1 10,20,22,0 * * ? ")
    public void scheduled() {
        List<Coupon> all = couponService.findAll(null);
        for (Coupon coupon : all
                ) {
            if (StringUtils.isEmpty(coupon.getIds())) {


                Integer id = null;
                Double count = 0.0;
                String ids = "";
                Integer num = 0;
                if (compare_date(coupon.getCollectTime(), DateUtils.getDate1()) == -1) {
                    CouponOrder couponOrder = new CouponOrder();
                    couponOrder.setCid(coupon.getId());
                    List<CouponOrder> all1 = couponOrderService.findAll(couponOrder);
                    for (CouponOrder coupono : all1
                            ) {
                        count += coupono.getNum();
                    }
                    CouponDrool couponDrool = new CouponDrool();
                    couponDrool.setCid(coupon.getId());
                    List<CouponDrool> all2 = couponDroolService.findAll(couponDrool);
                    for (int i = 0; i < all2.size(); i++) {
                        if (all2.get(i).getPeople() >= all1.size()) {
                            id = i;
                        }
                    }
                    if (id != null) {
                        CouponDrool couponDrool1 = all2.get(id);
                        Double v = count / 100 * (100.0 - couponDrool1.getRateRobot());
                        for (int i = 0; i < all1.size(); i++) {
                            for (int y = 0; y < all1.get(i).getNum(); y++) {
                                if (num <= v) {
                                    CouponOrder couponOrder1 = all1.get(i);
                                    couponOrder1.setStatus(1);
                                    couponOrderService.update(couponOrder1);
                                    UserCoupon userCoupon = new UserCoupon();
                                    userCoupon.setUserId(couponOrder1.getUserId());
                                    userCoupon.setCid(couponOrder1.getCid());
                                    userCoupon.setStatus(0);
                                    userCouponService.save(userCoupon);
                                    num++;
                                    ids += couponOrder1.getUserId() + ",";
                                }
                            }
                        }
                        Double b = count / 100 * (100.0 - couponDrool1.getRate());
                        for (int i = 0; i < b; i++) {
                            ids += "NA,";
                        }
                    }
                    String substring = ids.substring(0, ids.length() - 1);
                    coupon.setIds(substring);
                    couponService.update(coupon);
                }
            }
        }
    }

    @Scheduled(cron = "0 0 * * * ? ")
    public void scheduled1() {
        try {
            List<Orders> all = ordersService.findAll(null);
            for (Orders order : all
                    ) {
                if (order.getStatus().equals("0")) {

                    DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    Date d1 = df.parse("2005-03-26 13:31:40");

                    Date d2 = df.parse("2004-01-02 11:30:24");

                    long diff = d1.getTime() - d2.getTime();//这样得到的差值是微秒级别

                    long hour = (diff / (60 * 60 * 1000));

                    if (hour >= 12) {
                        Orders orders = new Orders();
                        orders.setId(order.getId());
                        orders.setStatus("5");
                        Integer update = ordersService.update(orders);
                        System.out.println(update);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


}
